﻿using System;
using System.Windows.Forms;
using IGM.DataProvider;
using IGM.DataProvider.Entites;
using IGM.Framework.Commons;
using IGM.Presentation.Main;

namespace IGM.Presentation.Users
{
    public partial class UsersControl : UserControl
    {
        private readonly WcfService.DomainService _service;
        
        public UsersControl()
        {
            InitializeComponent();
            _service = MainView.Instance.IgmService;
        }

        private void UserInfoControlLoad(object sender, EventArgs e)
        {
            LoadUsers();
            Dock = DockStyle.Top;
        }

        private void LoadUsers()
        {
            dgvUsers.DataSource = _service.GetUsers();

            //DataGridViewHelper.ShowHideColumns(dgvUsers, new[]
            //                                                       {
            //                                                           (string) typeof (User).Properties().IsChecked,
            //                                                           (string) typeof (User).Properties().Username
            //                                                       });
            DataGridViewHelper.ShowIsCheck(dgvUsers);
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvUsers, false);

            UserCurrentRowChanged();
        }

        private void UserCurrentRowChanged()
        {
            if (dgvUsers.CurrentRow != null)
                txtUser.Text = dgvUsers.CurrentRow.Cells[(string)typeof(User).Properties().Username].Value.ToString();
            else
            {
                txtUser.Text = string.Empty;
                ckbAllUser.Checked = false;
            }
        }

        /// <summary>
        /// when cell content click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DgvUsersCellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvUsers == null ||
               dgvUsers.Columns[(string)typeof(User).Properties().IsChecked] == null ||
               dgvUsers.Columns[(string)typeof(User).Properties().IsChecked].Index != e.ColumnIndex)
                return;

            dgvUsers[e.ColumnIndex, e.RowIndex].Value =
                !(bool)dgvUsers[e.ColumnIndex, e.RowIndex].Value;

            ckbAllUser.Checked = DataGridViewHelper.AllRowsIsCheck(dgvUsers);
        }

        private void DgvUsersCurrentCellChanged(object sender, EventArgs e)
        {
            UserCurrentRowChanged();
        }

        private void CkbAllUserCheckedChanged(object sender, EventArgs e)
        {
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvUsers, ckbAllUser.Checked);
        }

        private void BtnAddUserClick(object sender, EventArgs e)
        {
            try
            {
                //var user = new User
                //               {
                //                   Permission = 0,
                //                   Name = txtUser.Text.Trim(),
                //                   Password = txtPass.Text.Trim()
                //               };
                //string message;

                //if (_business.AddNewFacultyValidator(user, out message))
                //{
                //    _business.AddNewFaculty(user);

                //    ReloadFaculty(user.Name);
                //}
                //else
                //    MessageBox.Show(message);
            }
            catch (Exception ex)
            {
                Logger.Write(null, ex);
            }
        }

    }
}
